/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.nodes.behavior.delegates;

import com.android1.amarena2d.engine.AmarenaCamera;
import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.input.EntityTouchSpot;
import com.android1.amarena2d.input.TouchListener;
import com.android1.amarena2d.input.TouchSpot;
import com.android1.amarena2d.nodes.behavior.Touchable;

public class EntityTouchDelegate extends EngineObject implements TouchDelegate<EntityTouchSpot> {

    protected EntityTouchSpot touchspot;
    protected Touchable<EntityTouchSpot> entity;

    public EntityTouchDelegate(Touchable<EntityTouchSpot> entity) {
        this.entity = entity;
    }

    @Override
    public EntityTouchDelegate makeTouchable(int priority, TouchListener listener) {
        if (touchspot == null) {
            touchspot = new EntityTouchSpot(entity, priority);
            touchspot.register();
        }
        touchspot.addListener(listener);

        return this;
    }

    @Override
    public EntityTouchDelegate makeTouchable(TouchListener listener) {
        return makeTouchable(TouchSpot.NO_PRIORITY, listener);
    }


    @Override
    public TouchListener addListener(TouchListener listener) {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        return touchspot.addListener(listener);
    }

    @Override
    public void removeListener(TouchListener listener) {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        touchspot.remove(listener);
    }

    @Override
    public void revokeTouchable() {
        if (touchspot != null) {
            engine.getTouchSpotSystem().unregister(touchspot);
            touchspot = null;
        }
    }

    @Override
    public EntityTouchSpot getTouchspot() {
        return touchspot;
    }

    @Override
    public void pause() {
        if (touchspot != null) {
            touchspot.setActive(false);
        }
    }

    @Override
    public void resume() {
        if (touchspot != null) {
            touchspot.setActive(true);
        }
    }

    @Override
    public boolean isPaused() {
        return touchspot == null || !touchspot.isActive();
    }

    @Override
    public boolean isActive() {
        return !isPaused();
    }

    @Override
    public EntityTouchDelegate setTranslateToWorld() {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        touchspot.setTranslateToWorld(true);
        return this;
    }

    @Override
    public EntityTouchDelegate setTranslateToScreen() {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        touchspot.setTranslateToWorld(false);
        return this;

    }

    @Override
    public EntityTouchDelegate setNotifyNoHit(boolean notifyNoHit) {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        touchspot.setNotifyNoHits(notifyNoHit);
        return this;

    }

    @Override
    public boolean isNotifyNoHit() {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        return touchspot.isNotifyNoHits();
    }

    @Override
    public boolean isAlwaysNotifyAllListener() {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        return touchspot.isAlwaysNotifyAllListener();
    }

    @Override
    public TouchDelegate setAlwaysNotifyAllListener(boolean notifyAllListener) {
        if (touchspot == null)
            throw new IllegalStateException("No Touchspot has been created yet.");
        touchspot.setAlwaysNotifyAllListener(notifyAllListener);
        return this;
    }


}
